<script setup>
import { computed } from 'vue'
import { useCartStore } from '@/store/cart'
import CartItem from '@/components/CartItem.vue'
import PaymentModal from '@/components/PaymentModal.vue'
import { ref } from 'vue'

const cartStore = useCartStore()
const showPaymentModal = ref(false)

const cartItems = computed(() => cartStore.items)
const cartTotal = computed(() => cartStore.cartTotal)
const cartItemCount = computed(() => cartStore.cartItemCount)

const removeItem = (productId) => {
  cartStore.removeFromCart(productId)
}

const updateQuantity = (productId, quantity) => {
  cartStore.updateQuantity(productId, quantity)
}

const proceedToCheckout = () => {
  if (cartItemCount.value > 0) {
    showPaymentModal.value = true
  }
}

const closePaymentModal = () => {
  showPaymentModal.value = false
}

const completePayment = () => {
  cartStore.clearCart()
  closePaymentModal()
}
</script>

<template>
  <div class="cart-view">
    <h1>购物车</h1>
    
    <div v-if="cartItemCount > 0" class="cart-container">
      <div class="cart-items">
        <CartItem 
          v-for="item in cartItems" 
          :key="item.id" 
          :item="item"
          @remove="removeItem"
          @update-quantity="updateQuantity"
        />
      </div>
      
      <div class="cart-summary">
        <h2>订单摘要</h2>
        <div class="summary-row">
          <span>商品总数:</span>
          <span>{{ cartItemCount }} 件</span>
        </div>
        <div class="summary-row total">
          <span>总计:</span>
          <span>¥{{ cartTotal.toFixed(2) }}</span>
        </div>
        
        <button class="checkout-btn" @click="proceedToCheckout">
          结算订单
        </button>
      </div>
    </div>
    
    <div v-else class="empty-cart">
      <div class="empty-message">
        <h2>您的购物车是空的</h2>
        <p>快去添加一些商品吧！</p>
        <router-link to="/" class="shop-btn">继续购物</router-link>
      </div>
    </div>
    
    <PaymentModal 
      v-if="showPaymentModal"
      :total="cartTotal"
      @close="closePaymentModal"
      @complete="completePayment"
    />
  </div>
</template>

<style scoped>
.cart-view {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

h1 {
  font-size: 2rem;
  margin-bottom: 30px;
  color: #2c3e50;
  border-bottom: 2px solid #3498db;
  padding-bottom: 10px;
}

.cart-container {
  display: flex;
  gap: 30px;
}

.cart-items {
  flex: 3;
}

.cart-summary {
  flex: 1;
  background: #f8f9fa;
  border-radius: 10px;
  padding: 25px;
  height: fit-content;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
}

.cart-summary h2 {
  font-size: 1.5rem;
  margin-bottom: 20px;
  color: #2c3e50;
}

.summary-row {
  display: flex;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid #eee;
}

.summary-row.total {
  font-size: 1.3rem;
  font-weight: bold;
  border-bottom: none;
  margin-top: 10px;
  color: #e74c3c;
}

.checkout-btn {
  width: 100%;
  padding: 15px;
  background: #27ae60;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 1.1rem;
  font-weight: bold;
  margin-top: 20px;
  cursor: pointer;
  transition: background 0.3s;
}

.checkout-btn:hover {
  background: #219653;
}

.empty-cart {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 60vh;
}

.empty-message {
  text-align: center;
  background: #f8f9fa;
  padding: 40px;
  border-radius: 10px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
}

.empty-message h2 {
  font-size: 1.8rem;
  color: #2c3e50;
  margin-bottom: 15px;
}

.empty-message p {
  font-size: 1.2rem;
  color: #7f8c8d;
  margin-bottom: 25px;
}

.shop-btn {
  display: inline-block;
  padding: 12px 30px;
  background: #3498db;
  color: white;
  text-decoration: none;
  border-radius: 4px;
  font-weight: bold;
  transition: background 0.3s;
}

.shop-btn:hover {
  background: #2980b9;
}
</style>